package com.example.websocket.controller;

import com.example.websocket.model.table.User;
import com.example.websocket.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by liulanhua on 2018/9/25.
 */
@Slf4j
@RestController
public class IndexController {

    @Autowired
    UserService userService;

    @GetMapping(value = "")
    public ModelAndView index(ModelAndView mv) {
        mv.setViewName("index");
        mv.addObject("record", "Hello, Please login !");
        return mv;
    }

    @GetMapping(value = "/login")
    public ModelAndView loginPage(ModelAndView mv) {
        mv.setViewName("login");
        mv.addObject("record", "Hello, this is login page !");
        return mv;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public ResponseEntity<Map<String,Object>> login(@RequestBody User userReq) {
        User findUser = userService.findUser(userReq);
        Map<String,Object> result = new HashMap<>();
        result.put("user", findUser);
        int code = HttpStatus.NOT_FOUND.value();
        if (null != findUser && findUser.getPassword().equals(userReq.getPassword())) {
            code = HttpStatus.OK.value();
        }
        result.put("code", code);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


}
